/*
 * Parser.cpp
 *
 *  Created on: 27/08/2013
 *      Author: hijobobo
 */
using namespace std;
#include <iostream>
#include <stdio.h>
#include <string>
#include <fstream>
#include <cstring>
#include "Parser.h"
#include "List.h"

Parser::Parser(string filePathArgument) {

	hasNext = true;
	wordList = new List<char*>;
	openFile(filePathArgument,file);
	currentLine();

}

Parser::Parser() {

	hasNext = true;
	wordList = new List<char*>;

}


Parser::~Parser() {
	delete wordList;
}

void Parser::currentLine() {

	string line;
	char* str;
	char* word;

	if (getline(file, line)) {

		str=new char[line.size()+1];
	   	str[line.size()]=0;
	   	memcpy(str,line.c_str(),line.size());
	   	word = strtok(str, " ,./_-()¿?¡!\"'$%&()[]{}1234567890\\:;"); //Después le voy a agregar en el constructor la opción de mandarle los tokens.

	   	while (word != NULL) {
    		wordList->insert(word);
    		word = strtok(NULL, " ,./_-()¿?¡!\"'$%&()[]{}1234567890\\:;");
    	}

	}

	else {
			hasNext = false;
	}
}

void Parser::openFile(string pathArgument, ifstream& fileToOpen) {

	fileToOpen.open( pathArgument.c_str() , ifstream::in );

}

void Parser::setFile(string pathArgument) {

	this->openFile( pathArgument , file );
	hasNext = true;

}

void Parser::closeFile() {

	file.close();

}

char* Parser::nextWord() {

	while ( wordList->lista_vacia() && (hasNextWord())) {
		this->currentLine();
	}

	char* word;

	if (hasNextWord()) {
	word = wordList->get_dato(1);
	wordList->del_dato(1);
	}

	while ( wordList->lista_vacia() && (hasNextWord())) {
		this->currentLine();
	}

	return word;

}

bool Parser::hasNextWord() {	return hasNext;	}

List<char*>* Parser::getList() {	return wordList;	}
